/**
 * 24/02/2014
 * 
 * IssuetypeDAO
 */
package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Issuetype;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of IssuetypeDAO
 * @author Javier Tejedor
 */
public class IssuetypeDAO implements IssuetypeDAOInterface {

	@Autowired
	private SessionFactory sessionFactory;

	/**
	 * Constructor
	 */
	public IssuetypeDAO () {

	}

	/**
	 * returns Hibernate session
	 * @return
	 */
	private Session getSession () {
		return sessionFactory.getCurrentSession();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#selectById(int)
	 */
	@Transactional(readOnly = true)
	public Issuetype selectById(int id) {
		Issuetype issuetype = (Issuetype) getSession().get(Issuetype.class, id);
		return issuetype;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#selectAll()
	 */
	@Transactional(readOnly = true)
	public List<Issuetype> selectAll() {
		List<Issuetype> issuetype = getSession().createQuery("from Issuetype").list();
		return issuetype;
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#selectAllCount()
	 */
	@Transactional(readOnly = true)
	public int selectAllCount() {
		List<Issuetype> issuetype = getSession().createQuery("from Issuetype").list();
		return issuetype.size();
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#insert(org.cuatrovientos.erp.model.Issuetype)
	 */
	@Transactional
	public void insert(Issuetype issuetype) {
		Integer id = (Integer) getSession().save(issuetype);
		issuetype.setId(id);           
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#update(org.cuatrovientos.erp.model.Issuetype)
	 */
	@Transactional
	public void update(Issuetype issuetype) {
		getSession().merge(issuetype);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#delete(org.cuatrovientos.erp.model.Issuetype)
	 */
	@Transactional
	public void delete(Issuetype issuetype) {
		getSession().delete(issuetype);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#saveOrUpdate(org.cuatrovientos.erp.model.Issuetype)
	 */
	@Transactional
	public void saveOrUpdate(Issuetype issuetype) {
		getSession().saveOrUpdate(issuetype);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#deleteById(int)
	 */
	@Transactional
	public void deleteById(int id) {
		Issuetype issuetype = (Issuetype) getSession().load(Issuetype.class,id);
		getSession().delete(issuetype);
	}

	/* (non-Javadoc)
	 * @see org.cuatrovientos.erp.dao.IssuetypeDAOInterface#search(java.lang.String)
	 */
	@Transactional(readOnly = true)
	public List<Issuetype> search(String name) {
		Query query = 
				getSession().
				createQuery("from Issuetype s where s.name like :name");
		
		query.setParameter("name", "%" + name + "%");

		List<Issuetype> issuetype = query.list();

		return issuetype;
	}

}
